//
//  RookieCell.swift
//  ClickTheCellExpand
//
//  Created by Rookie_YX on 16/7/1.
//  Copyright © 2016年 YX_Rookie. All rights reserved.
//

import UIKit
import SnapKit

enum cellType:Int {
  case TitleType
  case CellDetilType
}

class RookieCell: UITableViewCell {
  
  var isExpand:Bool = false  // 默认NO
  var type:cellType = .TitleType // 默认TitleType
  var infoDic:String = "标题" // 默认标题
  
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  // 加载视图
  func setupTitleUI(title:String){
    
    contentView.addSubview(titleLabel)
    contentView.addSubview(imgView)
    titleLabel.text = title
    if isExpand {
      imgView.image = UIImage(named: "icon_arrow_down")
    }
    else{
      imgView.image = UIImage(named: "icon_arrow_up")
    }
  }
  
  func setupDetilUI(detil:String){
    
    contentView.addSubview(detilLabel)
    detilLabel.text = detil
    
  }
  
  // 布局
  func setLayoutUI(type:cellType){
    switch type {
    case .TitleType:
      setLayoutTitle()
   case .CellDetilType:
      setLayoutDetil()
    }
  }
  
  private func setLayoutTitle(){
    
    titleLabel.snp_makeConstraints { (make) in
      make.top.bottom.equalTo(contentView)
      make.left.equalTo(contentView).offset(10)
      make.right.equalTo(imgView.snp_left).offset(-10)
    }
    imgView.snp_makeConstraints { (make) in
      make.top.bottom.equalTo(titleLabel)
       make.width.equalTo(40)
      make.left.equalTo(titleLabel.snp_right).offset(0)
      make.right.equalTo(contentView).offset(-5)
    }
  }
  private func setLayoutDetil(){
    detilLabel.snp_makeConstraints { (make) in
      make.top.equalTo(contentView)
      make.left.equalTo(contentView).offset(10)
      make.right.equalTo(contentView).offset(-10)
    }
  }
  // 高度
  class func cellDetilHeight(str:String) ->CGFloat{
    
    let font = UIFont.systemFontOfSize(13)
    let size = CGSizeMake(UIScreen.mainScreen().bounds.size.width - 20, CGFloat.max)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .ByWordWrapping
    let attributes = [NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy()]
    let rect = str.boundingRectWithSize(size, options:.UsesLineFragmentOrigin, attributes: attributes, context:nil)
    return rect.size.height + 5
  }
  // MARK:- UI
  private lazy var titleLabel:UILabel = {
    let lab = UILabel()
    lab.textColor = UIColor.orangeColor()
    lab.font = UIFont.systemFontOfSize(15)
    return lab
  }()
  private lazy var detilLabel:UILabel = {
    let lab = UILabel()
    lab.textColor = UIColor.grayColor()
    lab.font = UIFont.systemFontOfSize(13)
    lab.numberOfLines = 0
    return lab
  }()
  private lazy var imgView: UIImageView = {
    let iv = UIImageView()
    iv.contentMode = UIViewContentMode.ScaleAspectFit
    return iv
  }()

}

extension RookieCell{
  
}
